﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;

namespace DatabaseInterface
{
    class TextHandler
    {
        /// <summary>
        /// Language for texts
        /// </summary>
        public enum Language
        {
            /// <summary>
            /// English
            /// </summary>
            EN,
            /// <summary>
            /// Spanish
            /// </summary>
            ES,
            /// <summary>
            /// Portuguese
            /// </summary>
            PT

        }

        Connection cnx;
        public TextHandler(Connection cnx)
        {
            this.cnx = cnx;
        }
        public String getText(String textId, Language language)
        {
            String text = "";
            String langId = this.getLangId(language);

            String commandText = "SELECT TOP 1 value FROM Texts WHERE TextId = @TextId AND Language = @Language";
            SqlCommand cmd = new SqlCommand(commandText, this.cnx.getConnection());

            cmd.Parameters.Add("@TextId", System.Data.SqlDbType.VarChar, 50);
            cmd.Parameters.Add("@Language", System.Data.SqlDbType.Char, 2);
            cmd.Parameters["@TextId"].Value = textId;
            cmd.Parameters["@Language"].Value = langId;

            cnx.getConnection().Open();
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                text += dr[0].ToString();
            }
            cnx.getConnection().Close();

            if (text == "")
            {
                text = textId;
            }
            
            return text;
        }
        public String getLangId(Language language)
        {
            String langId = "";
            switch (language)
            {
                case Language.EN:
                    langId = "EN";
                    break;
                case Language.ES:
                    langId = "ES";
                    break;
                case Language.PT:
                    langId = "PT";
                    break;
                default:
                    langId = "ES";
                    break;
            }
            return langId;
        }

    }
}
